import { Manager } from "./manager.js";
import { Role } from "./parentAir.js";

export class Bullet extends Role{
    constructor(w,h,x,y,imgUrl,boomUrl,speed){
        super(w,h,x,y,imgUrl,boomUrl)
        this.speed = speed;
        this.move();
        this.hurt = 1;
    }
    move(){
        let _this = this;
        this.move_itimer = setInterval(() => {
            _this.item.offset({'top':_this.y-=3});
            _this.collide();
            if(_this.y <= -10){
                _this.item.remove();
                clearInterval(_this.move_itimer)
            } 

        }, 100/60);
    }
    collide(){
        let all = Manager.allEnemy;
        let [x,y,p_w,p_h] = [this.x,this.y,this.w,this.h];
        all.forEach((item)=>{
            let [f_x,f_y,f_w,f_h] = [item.x,item.y,item.w,item.h];
            if(x+p_w > f_x && f_x + f_w > x && y + p_h > f_y
                && f_h + f_y > y){
                this.item.remove();
                clearInterval(this.move_itimer);
                item.loseHp(this.hurt);
            }
        })
    }
}